package com.huaxin.hxcloud.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.main.LoginActivity;

/**
 * 用户登录异常时显示的对话框
 */
public class SecurityInfoSDialog extends Dialog {

    private String tip = null;
    private TextView mTextContent;
    private boolean showing = false;

    public SecurityInfoSDialog(Context context) {
        super(context, R.style.Simple_Dialog);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_security_info);
        setCanceledOnTouchOutside(false);

        findViewById(R.id.send_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
        findViewById(R.id.send_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relogin();
            }
        });

        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        //设置对话框的类型，保证在广播中可以弹出
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
        setCanceledOnTouchOutside(false);
        mTextContent = (TextView) findViewById(R.id.tip_info);
        if (tip != null) {
            mTextContent.setText(tip);
        }
    }

    /**
     * 显示对话框
     * @param tip
     */
    public void show(final String tip) {
        this.tip = tip;
        show();
    }

    @Override
    public void show() {
        if (showing) {
            return;
        }
        showing = true;
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        showing = false;
    }

    /**
     * 重新登录
     */
    public void relogin() {
        dismiss();
        Intent intent = new Intent(getContext(), LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("secure", 110);
        //携带安全信息到登录界面,如果登录界面不成功则要关闭activity栈中所有的activity
        getContext().startActivity(intent);
    }

    /**
     * 登录异常，退出登录，彻底杀死线程
     */
    public void exit() {
        dismiss();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        getContext().startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
